﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using DBSourceToolsLib;
using DBSourceToolsLib.SysObjects;
using DBConnectionLib;


namespace TestDBSourceToolsLibNUnit
{
    [TestFixture]
    [Ignore]
    public class TestScriptRunner
    {
        private System.IO.MemoryStream m_Stream = new System.IO.MemoryStream();
        private DBSourceToolsLib.ScriptRunner m_ScriptRunner;
        private void ClearResultBuilder()
        {
            this.m_Stream.Close();
            this.m_Stream = new System.IO.MemoryStream();
        }

        private void EventReceived(object source, ProgressEventArgs args)
        {
            lock (this.m_Stream)
            {
                string strMessage = args.Message;
                strMessage += Environment.NewLine;
                System.Text.Encoding oEncoder = System.Text.Encoding.Default;
                this.m_Stream.Write(oEncoder.GetBytes(strMessage), 0, strMessage.Length);
            }
        }

        [Test]
        public void TestRunScriptUsingStreams()
        {
            string controlFile = BaseDirectoryHelper.TestFiles_control_ScriptRunner + "\\InvalidQuery.txt";
            string outputFile = BaseDirectoryHelper.TestFiles_output + "\\InvalidQueryUsingStreams_results.txt";
            FileHelper oFileHelper = new FileHelper();
            oFileHelper.DeleteFilesInDir(BaseDirectoryHelper.TestFiles_output);

            Stubs.NewDatabaseProjectFormStub oStub = new TestDBSourceToolsLibNUnit.Stubs.NewDatabaseProjectFormStub();

            
            string sqlScript = @"use [Invalid_Database_z12]
                                        GONOGO
                                        exec non_existant_proc;";

            m_ScriptRunner = new ScriptRunner( oStub.GetDBSourceToolsTestDB(), false);
            m_ScriptRunner.ProgressEventPublisher += this.EventReceived;

            Helper_TestResultsToFile(oStub.GetDBSourceToolsTestDB(), sqlScript, outputFile, true);
            //file should contain text : "Msg 911, Level 16, State 1"
            //oFileHelper.CompareFiles(controlFile, outputFile);

            //            Helper_CompareFiles(controlFile, outputFile);

        }

        private void Helper_TestResultsToFile(Database oDatabase, string sqlScript, string outputFile
            , bool useStreams)
        {
            ClearResultBuilder();

            if (useStreams)
                m_ScriptRunner.RunScriptUsingStreams("Query1", sqlScript);
            else
                m_ScriptRunner.RunScriptUsingFile("Query1", sqlScript);
            m_ScriptRunner.Join();

            lock (this.m_Stream)
            {
                using (System.IO.StreamWriter outputWriter = new System.IO.StreamWriter(outputFile))
                {
                    this.m_Stream.WriteTo(outputWriter.BaseStream);
                }
            }

        }


    }
}
